//
//  Tools.m
//  iCarCenter
//
//  Created by Storm on 14-4-28.
//  Copyright (c) 2014年 Storm. All rights reserved.
//

#import "Tools.h"

@implementation Tools

+ (BOOL)isNumber:(NSString *)strValue
{
    if (strValue == nil || [strValue length] <= 0)
    {
        return NO;
    }

    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *filtered = [[strValue componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    if (![strValue isEqualToString:filtered])
    {
        return NO;
    }
    return YES;
}

+ (void)jumpWithVCType:(NSInteger)vcType{
    
    UIViewController *vc = nil;
    UINavigationController *navi = nil;
    UIViewController *visibleVC;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    // 获取 UIWindowLevelNormal 下的窗口
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    //获取vc
    if ([vc isKindOfClass:[UIViewController class]])
        vc = nextResponder;
    else
        vc = window.rootViewController;
    //获取模态跳转的最后一个页面
    vc = [self getPresentedViewController:vc];
    //tabbar-navi的结构
    if ([vc isKindOfClass:[UITabBarController class]]) {
        navi = (UINavigationController *)(((UITabBarController *)vc).selectedViewController);
        visibleVC = navi.visibleViewController;
    }
    //navi-tabbar的结构
    else if([vc isKindOfClass:[UINavigationController class]]){
        navi = ((UINavigationController *)vc);
        visibleVC = navi.visibleViewController;
    }
    else{
        visibleVC = vc;
    }
    //需要跳转的页面的一些处理（比如传参...）
    UIViewController *jumpVC = nil;
    switch (vcType) {
        default:
            break;
    }
    
    //跳转的一些处理 完全可以自定义
    if ([visibleVC isKindOfClass:jumpVC.class]){//需要跳转的页面就是当前页
        
    }else{//执行跳转
        //有导航控制器直接push过去
        if (visibleVC.navigationController == navi&&navi) {
            [visibleVC.navigationController pushViewController:jumpVC animated:YES];
        }
        //当前页无导航控制器可以直接模态过去
        else{
            [visibleVC presentViewController:jumpVC animated:YES completion:nil];
        }
    }
    
}

/**
 获取模态跳转的最后一个视图
 
 @param vc 控制器
 
 @return 结果控制器
 */
+ (UIViewController *)getPresentedViewController:(UIViewController *)vc{
    UIViewController *this_vc = vc.presentedViewController;
    if (this_vc) {
        return [self getPresentedViewController:this_vc];
    }
    return vc;
}

// 获取当前的viewcontroller

+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    
    id nextResponder = [frontView nextResponder];
    
    
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        
        result = nextResponder;
    
    else
        
        result = window.rootViewController;
    
    return result;
    
}

// 从UIView获取 满足一个日常的需求:在UITableviewcell里面的UIView模块里面，调用self.navigationcontroller pushviewcontroller推入一个新的viewcontroller，需要获取其上层的UIViewcontroller, 可以使用下面的方法:
+ (UIViewController *)sh_viewControllerWith:(UIResponder *)responder
{
    while ((responder = [responder nextResponder])){
        if ([responder isKindOfClass: [UIViewController class]]){
            return (UIViewController *)responder;
        }
    }
    return nil;
}



@end
